package com.wish.g01.finance.kafka;

import com.wish.g01.base.service.ICwHailianDealPerDayService;
import com.wish.g01.finance.utils.DateUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.annotation.TopicPartition;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ClassName KafkaConsumer
 * @Description TODO
 * @Author MQ
 * @Date 2022/6/7 19:47
 */
@Component
public class KafkaConsumer1 {

    @Autowired
    private ICwHailianDealPerDayService dealPerDayService;

    @KafkaListener(topicPartitions = @TopicPartition(topic = "test",partitions = {"0"}))
    public void kafkaConsumer(ConsumerRecord<String,String> data, Acknowledgment acknowledgment){
        String key = data.key();
        if (key!=""){
            Date yesterday = DateUtils.getDateBeforeDays(1, new Date());
            dealPerDayService.saveDealPerDay(yesterday);
        }
        //防止消息丢失
        acknowledgment.acknowledge();
    }
}
